package com.practice.J2E.Test;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpRequest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author kayn
 * @create 2023-09-15 下午 04:39
 */
public class UploadFileTest {


    /**
     * 文件本地地址
     */
    private static final String FILE_URL = "C:\\Users\\1\\Desktop\\壁纸\\img\\tom.png";
    /**
     * 文件名称
     */
    private static final String FILE_NAME = "air-print-bg-1.png";

    /**
     * 文件归档参数名
     */
    private static final String READ_PARAM = "readparam";

    /**
     * 上传地址
     */
    private static final String UPLOAD_URL = "http://wapi.bondex.com.cn:12360/DocumentApi/api/Qiniu/UploadCloud?bucket=fortest";

    /**
     * 上传并归档地址
     */
    private static final String UPLOAD_AND_STORE_URL = "http://wapi.bondex.com.cn:12360/DocumentApi/api/File/UploadAndStoreFile";

    public static void main(String[] args) throws Exception{

//        uploadFile();

        uploadAndStoreFile();

    }


    private static void uploadFile() throws FileNotFoundException {

        InputStream inputStream = new FileInputStream(FILE_URL);

        String responseString = HttpRequest

                .post(UPLOAD_URL)

                .form(FILE_NAME, IoUtil.readBytes(inputStream), FILE_NAME)

                .execute()

                .body();

        System.out.println(responseString);

    }

    private static void uploadAndStoreFile() throws FileNotFoundException {

        InputStream inputStream = new FileInputStream(FILE_URL);

        String responseString = HttpRequest

                .post(UPLOAD_AND_STORE_URL)

                .form("", IoUtil.readBytes(inputStream), "")

                .form(READ_PARAM, readParam)

                .timeout(1000 * 20)

                .execute()

                .body();

        System.out.println(responseString);

    }


    /**
     * 上传并归档报文
     */
    private static final String readParam = "{\n" +
            "    \"Head\":{\n" +
            "        \"SeqNo\":\"123424235344523\",\n" +
            "        \"SenderId\":\"kayn@bondex.com.cn\",\n" +
            "        \"SenderName\":\"测试上传并归档\",\n" +
            "        \"FolderId\":160,\n" +
            "        \"FolderName\":\"null\",\n" +
            "        \"CreateOpId\":\"IT012\",\n" +
            "        \"CreateUserId\":\"\",\n" +
            "        \"CreateUser\":\"集团资讯/王浩\",\n" +
            "        \"CreateTime\":\"2023-07-17 16:50:03\",\n" +
            "        \"LoginToken\":\"A8494B508A83665A78C8B04A66B6F41119CD90CDFB57A0EB8B384E0B4D62BDD7CBEABE9BD7DC3548CFD7D2115D217116EBB21484065969A22349D00503D90100604394AB555CD3A8437F133097D5096F311EE73711C4FC52A7F5F150164FE2761527F6446CED341FEF76F330CD7C5997908F5EC99EC9205BF76590AF28925236213056D16AE0EBA50ED26F478B07BE88DA5B409BB5BFC752\",\n" +
            "        \"Action\":\"Add\",\n" +
            "        \"Version\":\"1.0\"\n" +
            "    },\n" +
            "    \"Main\":{\n" +
            "        \"FileId\":0,\n" +
            "        \"UserCode\":\"kayn@bondex.com.cn\",\n" +
            "        \"FolderId\":129,\n" +
            "        \"FileExtension\":\"png\",\n" +
            "        \"FullName\":\"tom.png\",\n" +
            "        \"FileLastWriteTime\":\"2018-08-24 02:41:31\",\n" +
            "        \"FileCreationTime\":\"2018-08-24 02:41:31\",\n" +
            "        \"UploadTime\":\"2019-08-24T02:41:31.198Z\",\n" +
            "        \"CreateOpId\":\"IT012\",\n" +
            "        \"CreateUser\":\"集团资讯/王浩\",\n" +
            "        \"MimeType\":\"application/pdf\",\n" +
            "        \"Remarks\":\"\",\n" +
            "        \"FileKey\":[\n" +
            "            {\n" +
            "                \"KeyCode\":\"主单号\",\n" +
            "                \"KeyValue\":\"tomcat\"\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "}";

}
